<?php

namespace App\Repositories;

use App\Models\AccountRecord;
use App\Models\Member;
use Exception;

class AccountRecordRepository
{
    public function getPages(array $filter = [], int $pageSize = 10, int $pageNum = 1)
    {
        $res = AccountRecord::query()
            ->where("uid", "=", $filter["wechat_admin_id"])
            ->where("is_delete", "=", 1)
            ->where("create_time", ">", date("Y-m-d H:i:s", strtotime("-3 months")))
            ->when(intval($filter['type']), function ($query) use ($filter) {
                return $query->where("inout", "=", intval($filter['type']) - 1);
            })
            ->orderBy('id', 'desc')
            ->paginate($pageSize, "*", null, $pageNum)
            ->toArray();

        $res = filterPaginateData($res);
        foreach ($res['items'] as $k => $v) {
            $res['items'][$k]['type_name'] = AccountRecord::account_record_type($v['type']);
        }

        return $res;
    }

    public function getOne($id = NULL)
    {
        if (!$id) return NULL;
        $res = AccountRecord::where("id", "=", $id)->first();
        $res['type_name'] = AccountRecord::account_record_type($res['type']);
        if (!in_array($res['type'], [3, 4, 16, 17, 18, 19, 52, 57])) {// 抽取微信二维码 不显示 对方昵称
            $res['df_uid'] && $res['df_username'] = Member::where("id", "=", $res['df_uid'])->value("username");
        }

        return $res;
    }
}

